package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.dao.CompanyModelCategoryDao;
import com.yenlien.traceyun.control.dao.CustomAppVersionCategoryDao;
import com.yenlien.traceyun.control.entity.CustomAppVersionCategory;
import com.yenlien.traceyun.control.service.rest.AppRestService;
import com.yenlien.traceyun.control.service.rest.ResourceRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-29
 * @description:
 **/
@RestController
@RequestMapping("/control/app")
public class AppController extends BaseController {

    @Autowired
    private AppRestService appRestService;

    @Autowired
    private ResourceRestService resourceRestService;

    @Autowired
    private CompanyModelCategoryDao companyModelCategoryDao;

    @Autowired
    private CustomAppVersionCategoryDao customAppVersionCategoryDao;

    //所以产品
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Object list() {
        return appRestService.list();
    }

    /**
     * 更改启用禁用状态
     *
     * @param id
     * @param used
     * @return
     */
    @RequestMapping(value = "/used/{id}/{used}", method = RequestMethod.GET)
    public Object used(@PathVariable("id") String id, @PathVariable("used") boolean used) {
        return appRestService.used(id, used);
    }

    /**
     * 根据appid获取app下面的所有权限
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/menus/{id}")
    public Object menus(@PathVariable("id") String id) {
        return resourceRestService.findByAppId(id);
    }

    /**
     * 向app下添加菜单
     *
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "/add/resource")
    public Object addMenu(@RequestBody JSONObject jsonObject) {
        CheckParams.checkJson(jsonObject, new String[]{"appId", "name", "router", "url"});
        return resourceRestService.save(jsonObject);
    }

    /**
     * 向app下添加菜单
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/dele/resource/{id}")
    public Object deleMenu(@PathVariable("id") String id) {
        return resourceRestService.dele(id);
    }

    //所以自定义产品下拉
    @RequestMapping(value = "/customApp/findAll", method = RequestMethod.GET)
    public Object findAll() {
        return appRestService.findAll();
    }

    //所以自定义产品列表
    @RequestMapping(value = "/list/customapp", method = RequestMethod.POST)
    public Object findBycustomapp(@RequestBody JSONObject jsonObject) {
        return appRestService.findCustomApp(jsonObject);
    }


    //添加自定义app
    @PostMapping("/createcustomapp")
    public Object createCustomApp(@RequestParam Map<String, Object> param, HttpServletRequest request) {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        return appRestService.createCustomApp(param, files);
    }



    @PostMapping("/createCustomApp2")
    public Object createCustomApp2(@RequestBody Map<String, Object> param, HttpServletRequest request) {
        return appRestService.createCustomApp2(param);
    }

    /**
     * 配置自定义app应用
     *
     * @param params
     * @return
     */
    @PostMapping(value = "/create/customappversion")
    public Object createCustomappversion(@RequestBody JSONObject params) {

        JSONArray modelclass = params.getJSONArray("categories");
        String customAppVersionId = params.getString("customAppVersionId");
        customAppVersionCategoryDao.deleteByCustomAppVersionId(customAppVersionId);
        if (modelclass != null && modelclass.size() > 0) {
            List<CustomAppVersionCategory> cms = new ArrayList<>();
            for (int i = 0; i < modelclass.size(); i++) {
                CustomAppVersionCategory customAppVersionCategory = new CustomAppVersionCategory();
                customAppVersionCategory.setCustomAppVersionId(customAppVersionId);
                customAppVersionCategory.setCategoryId(modelclass.getString(i));
                cms.add(customAppVersionCategory);
            }
            customAppVersionCategoryDao.saveAll(cms);
        }

        return appRestService.createCustomAppVersion(params);
    }

    /**
     * 根据自定义customappid，查询版本
     *
     * @param customappid
     * @return
     */
    @GetMapping(value = "/list/customappversion/{customappid}")
    public Object findCustomAppVersion(@PathVariable("customappid") String customappid) {
        return appRestService.findByCustomAppId(customappid);
    }


    /**
     * @return
     */
    @GetMapping(value = "/listall/customapp")
    public Object listallCustomapp() {
        return appRestService.listallCustomApp();
    }


    //根据自定义版本Id查询自定义产品
    @RequestMapping(value = "/findCustomAppVersionId/{id}", method = RequestMethod.GET)
    public Object findCustomAppVersionId(@PathVariable("id") String id) {
        return appRestService.findCustomAppVersionId(id);
    }


    /**
     * 根据公司id查询自定义产品
     *
     * @return
     */
    @GetMapping(value = "/loadbycompanyId/{id}")
    public Object loadbycompanyId(@PathVariable("id") String id) {
        JSONObject result = appRestService.loadbycompanyId(id);
        return result;
    }

}
